package blockchain

import (
	"context"
	"fmt"
	"github.com/shopspring/decimal"
	"private-domain-overseas-service/app/business/api/internal/repository/blockchain"
	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/app/business/api/internal/types"
	"private-domain-overseas-service/pkg/common"
	"private-domain-overseas-service/pkg/xerr"
	"private-domain-overseas-service/tools/ent/hzblockchainwalletrecord"
	"private-domain-overseas-service/tools/ent/predicate"

	"github.com/zeromicro/go-zero/core/logx"
)

type BlockchainWalletRecordDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBlockchainWalletRecordDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BlockchainWalletRecordDetailLogic {
	return &BlockchainWalletRecordDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BlockchainWalletRecordDetailLogic) BlockchainWalletRecordDetail(req *types.BlockchainWalletRecordDetailRequest) (resp *types.BlockchainWalletRecordDetailResponse, err error) {
	blockchainWalletRecordDao := blockchain.NewBlockchainWalletRecordDao(l.ctx, l.svcCtx)

	// 百分之一
	baiDecimal := decimal.NewFromInt32(100)
	// 千分之一
	thousandDecimal := decimal.NewFromInt32(100)
	// 获取用户ID
	userID := common.JwtAuthUserId(l.ctx)

	// 查询钱包记录
	walletRecordConditions := make([]predicate.HzBlockchainWalletRecord, 0)
	walletRecordConditions = append(walletRecordConditions, hzblockchainwalletrecord.UserIDEQ(userID))
	walletRecordConditions = append(walletRecordConditions, hzblockchainwalletrecord.RecordNumberEQ(req.RecordID))
	record, err := blockchainWalletRecordDao.BlockchainWalletRecordFirst(walletRecordConditions)
	if err != nil {
		return nil, xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("查询积分失败:%v", err.Error()))
	}

	// 组装返回数据
	resp = &types.BlockchainWalletRecordDetailResponse{
		RecordTitle:                record.RecordTitle,
		RecordNumber:               record.RecordNumber,
		RecordHash:                 record.RecordHash,
		RecordIntegralAmount:       decimal.NewFromFloat(float64(record.RecordIntegralAmount)).Div(baiDecimal).StringFixed(2),
		RecordHzbCoin:              decimal.NewFromFloat(float64(record.RecordHzbCoin)).Div(baiDecimal).StringFixed(2),
		RecordRates:                decimal.NewFromFloat(float64(record.RecordRates)).Div(thousandDecimal).StringFixed(2),
		RecordServiceCharge:        decimal.NewFromFloat(float64(record.RecordServiceCharge)).Div(baiDecimal).StringFixed(2),
		RecordStatus:               record.RecordStatus,
		RecordRejectCause:          record.RecordRejectCause,
		RecordWalletAddress:        record.RecordWalletAddress,
		RecordTransferStatus:       record.RecordTransferStatus,
		RecordTransferCompleteTime: record.RecordTransferCompleteTime,
		RecordRejectTime:           record.RecordRejectTime,
		RecordArrivedTime:          record.RecordArrivedTime,
		RecordPassTime:             record.RecordPassTime,
		CreatedAt:                  record.CreatedAt,
	}

	return resp, nil
}
